/**
 * Created by yingzi on 2017-6-24.
 */

var SysExcel=(function ($) {
    var obj={
        exportExcel:exportExcel,
        exportAllExcel:exportAllExcel
    }
    return obj;

    /**
     * 获取头部的信息
     */
    function getHeaderData(gridID) {
        var headers = $('#'+gridID).bootstrapTable('getOptions').columns;
        return headers;
    }
    /**
     * 获取表列的信息
     */
    function getColumnData(gridID) {
        var columnData = $('#'+gridID).bootstrapTable('getVisibleColumns');

        var columnArr = [];
        //获得合计值
        var lists=$(".fixed-table-footer .th-inner");
        var listLen=lists.length;
        for(var i = 0 ; i < columnData.length ; i++){

            var obj     = columnData[i];
            if(listLen>0){
                var sumObj=lists[i];
            }
            if(obj.visible == false){
                continue;
            }
            var column  = {};
            column.width    = obj.width;
            column.field    = obj.field;
            column.title    = obj.title;
            column.fieldIndex=obj.fieldIndex;//字段顺序
            column.align    = typeof (obj.align) == 'undefined'?'left':obj.align;
            column.colspan  = typeof (obj.colspan) == 'undefined'?1:obj.colspan;
            column.rowspan  = typeof (obj.rowspan) == 'undefined'?1:obj.rowspan;
            //取合计信息
            if(sumObj!=null) {
                column.footer = sumObj.innerText;
            }
            columnArr.push(column);
        }
        return columnArr;
    }
    /**
     * 获得表格数据
     * */
    function getRowData(gridID) {
        var rowData = $('#'+gridID).bootstrapTable('getData');
        return rowData;
    }

    /**
     * 导出当前页
     * */
    function exportExcel(title,gridID,headArr,footerArr) {
        var headData=getHeaderData(gridID);
        var columnData=getColumnData(gridID);
        var rowData=getRowData(gridID);
        var postData = {
            title:title,
            headData:headData,//头部信息
            subTitles: headArr,//子标题信息
            columns:columnData,//列信息
            rows:rowData,//数据
            footer:footerArr//尾标题
        };
        var url=tsUtils.getRootPath()+"/exportOne";
        var form=$("<form>");//定义一个form表单
        form.attr("style","display:none");
        form.attr("accept-charset","UTF-8");
        form.attr("target","");
        form.attr("method","post");
        form.attr("action",url);
        var input1=$("<input>");
        input1.attr("type","hidden");
        input1.attr("name","fileData");
        input1.attr("value",JSON.stringify(postData));
        $("body").append(form);//将表单放置在web中
        form.append(input1);
        form.submit();
    }

    /**
     * 导出所有页
     * */
    function exportAllExcel(title,grid,sql,headArr,sumArr,footerArr) {

    }
})(jQuery);
